package cn.qzbook.controller;

import cn.qzbook.domain.User;
import cn.qzbook.service.UserService;
import com.alibaba.fastjson.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/select")
public class SelectUser extends HttpServlet {

    private UserService userService =  new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");
        System.out.println(name);
        Integer id = null;
        List<User> users = null;
        try {
            id = Integer.parseInt(name);
        } catch (NumberFormatException e) {
        }
        if (null != id){
            users = userService.selectUserByMapper(id,null);
        }else {
            users = userService.selectUserByJDBC(null,name);
        }
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().write(JSONArray.toJSONString(users));
    }
}
